"""
公共方法
"""
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By

from common.Log import Log


class BasePage(object):
    """公共方法的封装"""

    def __init__(self, driver):
        self.driver = driver
        self.logger = Log().logging()

    def find_element(self, *loc):
        """元素定位"""
        try:
            self.logger.info("通过"loc[0]"定位，元素是"loc[1])
            return self.driver.find_element(*loc)
        except:
            self.logger.error('元素定位失败')

    def click(self, *loc):
        """点击"""
        self.find_element(*loc).click()

    def clear(self, *loc):
        """清除搜索框"""
        self.find_element(*loc).clear()

    def send_mes(self, text, *loc):
        """键盘输入"""
        self.find_element(*loc).send_keys(text)

    def forword(self):
        """浏览器前进"""
        self.driver.forword()

    def back(self):
        """浏览器后退"""
        self.driver.back()

    def refresh(self):
        """浏览器刷新"""
        self.driver.refresh()

    def js(self, value='window.scrollTo(0, 10000)'):
        """滚动条操作"""
        self.driver.execute_script(value)

    def geturl(self):
        """获取当前的url地址"""
        return self.driver.current_url

    def quit(self):
        """退出浏览器"""
        self.driver.quit()


class BaiDu(BasePage):
    def __init__(self, driver):
        BasePage.__init__(self, driver)
        self.driver.get("https://www.baidu.com")

    def test(self):
        # 定位搜索框
        input = (By.ID, 'kw')
        btn = (By.ID, 'su')
        self.send_mes('python', input)
        self.click(*btn)
        sleep(3)
        self.logger.info("-----测试完成------")
        self.logger.info(f'当前的url是:self.geturl()')
        self.quit()


if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(10)
    BaiDu(driver).test()
















